package cn.iocoder.yudao.module.shop.admin.controller.admin;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.shop.admin.convert.ShopUserConvert;
import cn.iocoder.yudao.module.shop.admin.pojo.po.ShopUser;
import cn.iocoder.yudao.module.shop.admin.pojo.vo.ShopUserCreateReqVO;
import cn.iocoder.yudao.module.shop.admin.pojo.vo.ShopUserPageReqVO;
import cn.iocoder.yudao.module.shop.admin.pojo.vo.ShopUserRespVO;
import cn.iocoder.yudao.module.shop.admin.service.ShopUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserLastShopId;

@Tag(name = "管理后台 - 店铺商户")
@RestController
@RequestMapping("/shop/admin")
@Validated
public class ShopUserController {

    @Resource
    private ShopUserService shopUserService;
    @Resource
    private ShopInfoApi shopInfoApi;
    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建店铺商户")
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Long> createAdmin(@Valid @RequestBody ShopUserCreateReqVO createReqVO) {
        return success(shopUserService.createAdmin(createReqVO));
    }

    @GetMapping("/delete")
    @Operation(summary = "删除店铺商户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Boolean> deleteAdmin(@RequestParam("id") Long id) {
        shopUserService.deleteAdmin(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺商户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:admin:query')")
    public CommonResult<ShopUserRespVO> getAdmin(@RequestParam("id") Long id) {
        ShopUser admin = shopUserService.getAdmin(id);
        return success(BeanUtils.toBean(admin, ShopUserRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得店铺商户列表")
    @PreAuthorize("@ss.hasPermission('shop:admin:query')")
    public CommonResult<List<ShopUserRespVO>> getAdminList() {
        List<ShopUser> list = shopUserService.getAdminListByShopId(getLoginUserLastShopId());
        if (CollUtil.isEmpty(list)) {
            return success(new ArrayList<>());
        }
        Set<Long> userIds = convertSet(list, ShopUser::getMemberUserId);
        List<UserSimpleVO> users = memberUserApi.getUserSimpleList(userIds);
        Set<Long> shopIds = convertSet(list, ShopUser::getShopId);
        List<ShopInfoDTO> shops = shopInfoApi.getShopInfoList(shopIds);
        return success(ShopUserConvert.INSTANCE.convertList(list, users, shops));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺商户分页")
    @PreAuthorize("@ss.hasPermission('shop:admin:query')")
    public CommonResult<PageResult<ShopUserRespVO>> getAdminPage(@Valid ShopUserPageReqVO pageReqVO) {
        PageResult<ShopUser> pageResult = shopUserService.getAdminPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty());
        }
        Set<Long> userIds = convertSet(pageResult.getList(), ShopUser::getMemberUserId);
        Set<Long> shopIds = convertSet(pageResult.getList(), ShopUser::getShopId);
        List<UserSimpleVO> users = memberUserApi.getUserSimpleList(userIds);
        Map<Long, ShopInfoDTO> shops = shopInfoApi.getShopInfoMap(shopIds);
        return success(ShopUserConvert.INSTANCE.convertPage(pageResult, users, shops));
    }

}
